from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QMessageBox
from PyQt5.QtCore import QProcess
import sys


class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.__process = QProcess()
        self.__btn = QPushButton('start notepad')
        self.__init_ui()

        # 关联button clicked信号和对应的槽函数
        self.__btn.clicked.connect(self.__btn_clicked_handler)
        self.__process.started.connect(self.__process_started_handler)
        self.__process.finished.connect(self.__process_finished_handler)

    def __init_ui(self):
        my_layout = QHBoxLayout()
        my_layout.addWidget(self.__btn)
        self.setLayout(my_layout)

    def __btn_clicked_handler(self):
        '''
        处理button的clicked信号，打开notepad.exe
        '''
        # self.__process.startDetached('notepad.exe')
        path = "../test1/test114.py"
        self.__process.setProgram('python')
        self.__process.setArguments([path])
        self.__process.startDetached()

    def __process_started_handler(self):
        '''
        处理QProcess的started信号
        '''
        start_msg_box = QMessageBox.information(self,
                                                'info',
                                                '已成功打开记事本',
                                                QMessageBox.StandardButton.Yes,
                                                QMessageBox.StandardButton.Yes)

    def __process_finished_handler(self, exitCode, exitStatus):
        '''
        处理QProcess的finished信号，获取退出状态
        '''

        finished_msg_box = QMessageBox.information(self,
                                                   'info',
                                                   f'记事本已关闭, exitcode={exitCode}, exitStatus:{exitStatus}',
                                                   QMessageBox.StandardButton.Yes,
                                                   QMessageBox.StandardButton.Yes)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    my_widget = MyWidget()
    my_widget.show()
    sys.exit(app.exec_())